/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package documenteditor.controller;

import documenteditor.controller.exceptions.NonexistentEntityException;
import documenteditor.controller.exceptions.PreexistingEntityException;
import documenteditor.entidade.model.ClienteBrinde;
import documenteditor.entidade.model.ClienteBrindePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

/**
 *
 * @author gleidson.moura
 */
public class ClienteBrindeJpaController {

    public ClienteBrindeJpaController() {
        emf = Persistence.createEntityManagerFactory("FidyCardPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ClienteBrinde clienteBrinde) throws PreexistingEntityException, Exception {
        if (clienteBrinde.getIdClienteBrinde() == null) {
            clienteBrinde.setIdClienteBrinde(new ClienteBrindePK());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(clienteBrinde);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findClienteBrinde(clienteBrinde.getIdClienteBrinde()) != null) {
                throw new PreexistingEntityException("ClienteBrinde " + clienteBrinde + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ClienteBrinde clienteBrinde) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            clienteBrinde = em.merge(clienteBrinde);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ClienteBrindePK id = clienteBrinde.getIdClienteBrinde();
                if (findClienteBrinde(id) == null) {
                    throw new NonexistentEntityException("The clienteBrinde with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ClienteBrindePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ClienteBrinde clienteBrinde;
            try {
                clienteBrinde = em.getReference(ClienteBrinde.class, id);
                clienteBrinde.getIdClienteBrinde();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The clienteBrinde with id " + id + " no longer exists.", enfe);
            }
            em.remove(clienteBrinde);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ClienteBrinde> findClienteBrindeEntities() {
        return findClienteBrindeEntities(true, -1, -1);
    }

    public List<ClienteBrinde> findClienteBrindeEntities(int maxResults, int firstResult) {
        return findClienteBrindeEntities(false, maxResults, firstResult);
    }

    private List<ClienteBrinde> findClienteBrindeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from ClienteBrinde as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ClienteBrinde findClienteBrinde(ClienteBrindePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ClienteBrinde.class, id);
        } finally {
            em.close();
        }
    }

    public int getClienteBrindeCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from ClienteBrinde as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
